"""Pydantic schemas for book related operations."""

from __future__ import annotations

from datetime import datetime
from typing import List, Optional, Dict, Any

from pydantic import BaseModel, Field


class BookCreate(BaseModel):
    """Schema for creating a new book."""
    book_id: str = Field(min_length=1, max_length=64)
    title: str = Field(min_length=1, max_length=256)
    subtitle: Optional[str] = None
    author: str = Field(min_length=1, max_length=128)
    dynasty: str = Field(min_length=1, max_length=64)
    category: str = Field(min_length=1, max_length=64)
    description: Optional[str] = None
    total_chapters: int = Field(ge=0)
    total_characters: int = Field(ge=0)
    cover: Optional[str] = None
    difficulty: str = Field(default="中等")
    language: str = Field(default="古汉语")
    tags: Optional[List[str]] = None
    content: Optional[Dict[str, Any]] = None


class BookUpdate(BaseModel):
    """Schema for updating book information."""
    title: Optional[str] = None
    subtitle: Optional[str] = None
    description: Optional[str] = None
    cover: Optional[str] = None
    difficulty: Optional[str] = None
    tags: Optional[List[str]] = None


class BookOut(BaseModel):
    """Schema for book list output."""
    id: int
    book_id: str
    title: str
    subtitle: Optional[str]
    author: str
    dynasty: str
    category: str
    description: Optional[str]
    total_chapters: int
    total_characters: int
    cover: Optional[str]
    difficulty: str
    language: str
    tags: Optional[List[str]]
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class BookDetail(BookOut):
    """Schema for detailed book information including content."""
    content: Optional[Dict[str, Any]]


class BookSearchResult(BaseModel):
    """Schema for book search results."""
    items: List[BookOut]
    total: int
    page: int
    page_size: int
    total_pages: int


class BookCategory(BaseModel):
    """Schema for book categories."""
    name: str
    count: int